using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace Easy.Geo.Kml
{
    [XmlRoot(ElementName="kml",Namespace="http://earth.google.com/kml/2.2")]
    public class Kml
    {
        public Kml()
        {
            this._Document = new Document();
        }
        public Kml(string documentName)
        {
            this._Document = new Document(documentName);
        }
        private Document _Document;
        [XmlElement(ElementName="Document",Type=typeof(Document))]
        public Document Document
        {
            get { return this._Document; }
            set { this._Document = value; }
        }

        public void AddFeature(Feature feature)
        {
            this._Document.AddFeature(feature);
        }

        public void AddStyle(Style style)
        {
            this._Document.AddStyle(style);
        }

        public Style AddStyle(string id)
        {
            return this._Document.AddStyle(id);
        }

        public void Build(string fileName)
        {            
            FileStream file = File.Create(fileName);
            XmlSerializer xml = new XmlSerializer(typeof(Kml));           
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("","http://earth.google.com/kml/2.2");
            xml.Serialize(file, this, namespaces);
            file.Flush();
            file.Close();
        }

        public void Build(TextWriter writer)
        {
            XmlSerializer xml = new XmlSerializer(typeof(Kml));
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "http://earth.google.com/kml/2.2");
            xml.Serialize(writer, this, namespaces);
        }
    }
}
